#!/usr/bin/python
import re       # for format checking
import sys      # for messages
import random   # for randomisation
import readline # to allow use of arrow keys to access previous entries

# set up a global compiled regex for the dice format
reDiceFormat = re.compile('[0-9]+d[0-9]+')

def NdX():
	
	# welcome message
	sys.stderr.write("Input a dice roll as NdX (e.g. 3d6) or quit:\n")
	
	# loop until the user quits
	while True:
		
		# get the input 
		roll = raw_input(': ')
		# test for a user command to quit
		if roll == 'quit':
			sys.exit()
		else:
			# check the format of the roll is NdX
			if reDiceFormat.match(roll)==None:
				sys.stderr.write('Input not as dice format (e.g. 3d6):\n')
			else:
				# separate the N and X and convert to integers
				roll = [int(x) for x in roll.split('d')]
				# get N rolls in the range 1:X
				vals = [random.randrange(1, roll[1] + 1) for i in range(roll[0])]
				# export the values and the sum
				print vals, str(sum(vals))

if __name__ == "__main__":
	NdX()